import java.util.Comparator;
import java.util.PriorityQueue;
class Solution {
    class arrcmp implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    }
    public int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new arrcmp());
        //初始条件判断
        int[] ret = new int[k];
        if(arr == null || k == 0){
            return ret;
        }
        //创建有k个数的优先级队列（默认小根堆）
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }
        //遍历其他元素与大根堆的根节点进行比较
        for (int i = k; i < arr.length ; i++) {
            int peekval = priorityQueue.peek();
            //小于根节点则交换
            if(arr[i] < peekval ){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }
        //最后用数组存储
        int[] new_Arr  = new int[k];
        for (int i = 0;i < k; i++){

            new_Arr[i] = priorityQueue.poll();
        }
        return new_Arr;

    }
}